<?php
/**
 * PackageController class
 */
class LoginController extends VTController {
	public function __construct() {
		parent::__construct ();
		$this->smarty->assign ( 'isLoginPage', true );
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site. Return list of all members
	 *
	 * @return void
	 */
	public function indexAction() {
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Login/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/LoginMainLayout.tpl' );
	}
	
	/**
	 * Action when user login to the system
	 *
	 * @return next action
	 */
	public function loginAction() {
		$this->smarty->assign ( 'LOGIN_PAGE', true );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Login/index.tpl' );
		$email = $this->getPost ( 'email', '' );
		$password = $this->getPost ( 'password', '' );
		if (empty ( $email ) || empty ( $password )) {
			$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/LoginMainLayout.tpl' );
			return null;
		}
		
		$userDataService = new UserDataService ();
		$result = $userDataService->loginAdmin ( $email, $password );
		
		if ($result == SUCC) {
			$this->loginsuccessAction ();
		} else {
			$this->smarty->assign ( 'errorLoginMessage', 'Sorry, but we could not find login details using the information you supplied. Please try again!' );
			$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/LoginMainLayout.tpl' );
		}
	}
	
	/**
	 * Action when user login successfully
	 *
	 * @return next action
	 */
	public function loginsuccessAction() {
		$authentication = new Authentication ();
		
		if ($authentication->isAdministrator () == true || $authentication->getUserType() == ROLE_ACCOUNTANT || $authentication->getUserType() == ROLE_DEALMANAGER) {
			
			// Save to user online
			//$userSessionDataService = new UserSessionDataService();
			//$userSessionDataService->saveCurrentUser();
			

			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin';
			CommonUtils::redirect ( $url );
		} else {
			$this->smarty->assign ( 'errorLoginMessage', 'Sorry, Only administrator can access Administrator Panel. Please try again!' );
			$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/LoginMainLayout.tpl' );
		}
	
	}
}